require "Common/define"
require "Logic/Config"
require "Net/send_Msg"
require "proto/rankBattle/message/ReqSetRealManFuckFormationMessage"
require "proto/rankBattle/message/ReqRealManFuckInfoMessage"
require "proto/rankBattle/message/ReqRealManFuckUseItemMessage"
require "proto/rankBattle/message/ReqHappyFuckMessage"
require "proto/rankBattle/message/ReqRealManFuckRankListMessage"

RealManWarHandler = RealManWarHandler or {};


function RealManWarHandler.GetRealManFuckPushHandler(msg)
    if msg.state == 0 then
        logicMgr.RealManWarManager.setRealManWarInfo(msg);
        FireEvent(Config.EventType.Fresh_RealManRealManWar);
    end
end

function RealManWarHandler.GetRealManFuckInfoHandler(msg)
    logicMgr.RealManWarManager.setGetData(false);
    if msg.state == 0 then
        logicMgr.RealManWarManager.setRealManPlayerInfo(msg.playerInfo);
        logicMgr.RealManWarManager.OpenSoloBattle();
    end
end

--设置阵容
function RealManWarHandler.GetRealManFuckFormationHandler(msg)
    logicMgr.RealManWarManager.setGetData(false);
    if msg.state == 0 then
        logicMgr.RealManWarManager.setRealManPlayerInfo(msg);
        FireEvent(Config.EventType.Fresh_RealManFormation, true);
        RealManWarHandler.setRealManFormationTA(msg);
    else
        FireEvent(Config.EventType.Fresh_RealManFormation, false);
    end
end

--设置阵容及扫荡奖励埋点
function RealManWarHandler.setRealManFormationTA(msg)
    local setFormation = {}
    if msg.fuckMen ~= nil then
        for i = 1, 5 do
            local parId = msg.fuckMen[i];
            local modelId = 0;
            local level = 0;
            local star = 0;
            if parId ~= nil then
                local hero = logicMgr.HeroManager.getHeroDatabyPartnerId(parId, true);
                if hero ~= nil then
                    modelId = hero["partnerModelId"];
                    level = hero["level"];
                    star = hero["star"] / 10;
                end
            end
            setFormation["hero_id_"..i] = modelId;
            setFormation["hero_level_"..i] = level;
            setFormation["hero_star_"..i] = star;
        end
    end
    TAManager.TrackByDictionary("real_man_formation",setFormation);

    local realManSweep = {};
    local buff = {};
    local potion = {};
    realManSweep.step = tonumber(msg.step);
    if msg.reward ~= nil then
        for i = 1, #msg.reward do
            local localReward = msg.reward[i];
            local type = tonumber(localReward.type);
            local id = tonumber(localReward.mid);
            local num = tonumber(tostring(localReward.amount));
            if type == 2 then
                buff[id] = num;
            elseif type == 3 then
                potion[id] = num;
            end
        end
    end
    local potionId = {200621, 200622, 200623};
    for i = 1, 3 do
        local num = 0;
        num = buff[i] or 0;
        realManSweep["buff_"..i] = num;
        num = potion[potionId[i]] or 0;
        realManSweep["potion_"..i] = num;
    end
    TAManager.TrackByDictionary("real_man_sweep",realManSweep);
end

--战斗结束
function RealManWarHandler.GetHappyFuckFinishPushHandler(msg)
    if msg.state == 0 then
        logicMgr.RealManWarManager.setFightResult(msg);
        if not logicMgr.RealManWarManager.getAutoBattleState() and logicMgr.RealManWarManager.getPlayerOperationState() then
            FightManager.hasEntrance = false;
            FireEvent(Config.EventType.Fresh_RealManBattle, false);
            local battleResult = logicMgr.RealManWarManager.getFightResult();
            if battleResult ~= nil and battleResult.record ~= nil then
                logicMgr.RealManWarManager.setEnterBattle(true);
                BattlePlaybackManager.setBattleRecord(battleResult.record);
                BattlePlaybackManager.setBattleResult(battleResult.fightResult);
                BattlePlaybackManager.requestFightCmdThenPlay(battleResult.record, EnumConst.FightTypeConst.RealMan);
                AudioManager.Play(29);
            end
        else
            FireEvent(Config.EventType.Fresh_RealManBattle, true);
        end
        RealManWarHandler.setRealManBattleTA(msg);
    else
        FireEvent(Config.EventType.Fresh_RealManBattle, false);
    end
end

--战斗结束埋点
function RealManWarHandler.setRealManBattleTA(msg)
    local battleResult = {};
    local buff = {};
    local potion = {};
    if msg.playerInfo.buff ~= nil then
        for i = 1, #msg.playerInfo.buff do
            local localBuff = msg.playerInfo.buff[i];
            buff[tonumber(localBuff.mid)] = tonumber(tostring(localBuff.amount));
        end
    end
    if msg.playerInfo.potion ~= nil then
        for i = 1, #msg.playerInfo.potion do
            local localPotion = msg.playerInfo.potion[i];
            potion[tonumber(localPotion.mid)] = tonumber(tostring(localPotion.amount));
        end
    end
    local potionId = {200621, 200622, 200623};
    for i = 1, 3 do
        local num = 0;
        num = buff[i] or 0;
        battleResult["buff_"..i] = num;
        num = potion[potionId[i]] or 0;
        battleResult["potion_"..i] = num;
    end
    local type = 0;
    local id = 0;
    local num = 0;
    if msg.reward ~= nil then
        type = tonumber(msg.reward.type);
        id = tonumber(msg.reward.mid);
        num = tonumber(tostring(msg.reward.amount));
    end
    battleResult["get_buff_id"] = 0;
    battleResult["get_potion_id"] = 0;
    if type == 2 then
        battleResult["get_buff_id"] = id;
    elseif type == 3 then
        battleResult["get_potion_id"] = id;
    end
    TAManager.TrackByDictionary("real_man_battle_result", battleResult);
end

--使用药水
function RealManWarHandler.getRealManFuckUseItemHandler(msg)
    if msg.state == 0 then
        logicMgr.RealManWarManager.setRealManPlayerInfo(msg.playerInfo);
        FireEvent(Config.EventType.Fresh_RealManUseItem, true, tonumber(msg.item));
        RealManWarHandler.setRealManUseItemTA(msg);
    else
        FireEvent(Config.EventType.Fresh_RealManUseItem, false, 0);
    end
end

--使用药水埋点
function RealManWarHandler.setRealManUseItemTA(msg)
    local useItem = {};
    local potion = {};
    if msg.playerInfo.potion ~= nil then
        for i = 1, #msg.playerInfo.potion do
            local localPotion = msg.playerInfo.potion[i];
            potion[tonumber(localPotion.mid)] = tonumber(tostring(localPotion.amount));
        end
    end
    useItem["step"] = tonumber(msg.playerInfo.step);
    useItem["potion_id"] = tonumber(msg.item);
    local modelId = 0;
    if RealManWarHandler.partnerId ~= nil then
        local hero = logicMgr.HeroManager.getHeroDatabyPartnerId(RealManWarHandler.partnerId, true);
        if hero ~= nil then
            modelId = hero["partnerModelId"];
        end
    end
    useItem["hero_id"] = modelId;
    local potionId = {200621, 200622, 200623};
    for i = 1, 3 do
        local num = 0;
        num = potion[potionId[i]] or 0;
        useItem["potion_"..i] = num;
    end
    TAManager.TrackByDictionary("real_man_use_potion", useItem);
    RealManWarHandler.partnerId = nil;
end

function RealManWarHandler.getRealManFuckRankListHandler(msg)
    if msg.state == 0 then
        logicMgr.RealManWarManager.setRealManFuckRankList(msg);
    end
    FireEvent(Config.EventType.Fresh_RealManRank);
end



function RealManWarHandler.sendReqRealManFuckInfoMessage()
    local req = ReqRealManFuckInfoMessage.New();
    sendReqMessage(req);
end

function RealManWarHandler.sendReqSetRealManFuckFormationMessage(formation)
    local req = ReqSetRealManFuckFormationMessage.New();
    req.fuckMen = formation;
    sendReqMessage(req);
end

function RealManWarHandler.sendReqHappyFuckMessage(formation, isQuick)
    local req = ReqHappyFuckMessage.New();
    req.formation = formation;
    if isQuick then
        req.quickFuck = 1;
    else
        req.quickFuck = 0;
    end
    sendReqMessage(req);
end

function RealManWarHandler.sendReqRealManFuckUseItemMessage(partnerId, itemId)
    local req = ReqRealManFuckUseItemMessage.New();
    RealManWarHandler.partnerId = partnerId;
    req.partnerId = partnerId;
    req.item = itemId;
    sendReqMessage(req);
end

function RealManWarHandler.sendReqRealManFuckRankListMessage()
    local req = ReqRealManFuckRankListMessage.New();
    sendReqMessage(req);
end